package com.appota.vn.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.appota.vn.social.common.CommonStatic;
import com.appota.vn.util.CommonUtils;
import com.gsmvn.ngamgaixinh.JsonUtil;

public class HttpHelper {

	public static final String TAG = "HttpHelper";

	public static String post(String requestUrl,
			ArrayList<NameValuePair> nameValuePairs) {
		String s = "";
		HttpPost httpPost = new HttpPost(requestUrl);
		httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
		HttpClient client = HttpUtils.getNewHttpClient();
		HttpResponse response;
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			response = client.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				InputStream inputStream = response.getEntity().getContent();
				s = CommonUtils.inputStreamToString(inputStream);
				client.getConnectionManager().shutdown();
				client = null;
				return s;
			}
			client.getConnectionManager().shutdown();
			client = null;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			client = null;
		} catch (IOException e) {
			e.printStackTrace();
			client = null;
		}
		return s;
	}

	// public static String getAccesToken() {
	// String result = "";
	// String url_request = API.ACCESS_TOKEN_URL;
	// ArrayList<NameValuePair> nameValuePairs = addParameter();
	// result = post(url_request, nameValuePairs);
	// return result;
	//
	// }

	public static String get(String url_request) {
		String result = "";
		HttpGet httpGet = new HttpGet(url_request);
		HttpClient client = HttpUtils.getNewHttpClient();
		HttpResponse response;
		try {
			response = client.execute(httpGet);
			Log.d("", "res status " + response.getStatusLine().getStatusCode());
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				InputStream inputStream = response.getEntity().getContent();
				result = CommonUtils.inputStreamToString(inputStream);
				Log.d(TAG, result);
				client.getConnectionManager().shutdown();
				client = null;
				return result;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			client.getConnectionManager().shutdown();
			client = null;
		} catch (IOException e) {
			e.printStackTrace();
			client.getConnectionManager().shutdown();
			client = null;
		}
		return result;
	}

	public static String getRequestToken() {
		String result = "";
		String url_request = CommonStatic.REQUEST_TOKEN_URI;
		Log.d(TAG, url_request);
		result = get(url_request);
		Log.d(TAG, result);
		return result;
	}

	private static ArrayList<NameValuePair> addLoginParameter(Context context,
			String username, String password) {
		ArrayList<NameValuePair> rs = new ArrayList<NameValuePair>();
		rs.add(new BasicNameValuePair(CommonStatic.USER_NAME, username));
		rs.add(new BasicNameValuePair(CommonStatic.PASSWORD, password));
		rs.add(new BasicNameValuePair(CommonStatic.DEVICE_ID, CommonUtils
				.getDeviceId(context)));
		rs.add(new BasicNameValuePair(CommonStatic.DEVICE_OS, CommonUtils
				.getDeviceOs()));
		rs.add(new BasicNameValuePair(CommonStatic.DEVICE_OS_VERSION,
				CommonUtils.getDeviceVersion()));
		rs.add(new BasicNameValuePair(CommonStatic.VENDOR, CommonUtils
				.getVendor()));
		rs.add(new BasicNameValuePair(CommonStatic.PHONE_NUMBER, CommonUtils
				.getPhoneNumber(context)));
		rs.add(new BasicNameValuePair(CommonStatic.CLIENT_VERSION, CommonUtils
				.getClientVersion(context) + ""));
		return rs;
	}

	public static String requestPopUp(String accestoken) {
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("store", "android"));
		list.add(new BasicNameValuePair("page", "popup_detail"));
		list.add(new BasicNameValuePair("section", "header"));
		list.add(new BasicNameValuePair("position", "1"));
		list.add(new BasicNameValuePair("type", "web"));
		list.add(new BasicNameValuePair("numOfBanners", "1"));
		String url = "https://api.appota.com/banner/get?access_token="
				+ accestoken + "&lang=vi";
		String rs = post(url, list);

		return rs;
	}

	public static String requestBanner(String access_token) {
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("store", "android"));
		list.add(new BasicNameValuePair("page", "banner_detail"));
		list.add(new BasicNameValuePair("section", "header"));
		list.add(new BasicNameValuePair("position", "1"));
		list.add(new BasicNameValuePair("type", "web"));
		list.add(new BasicNameValuePair("numOfBanners", "1"));
		String url = "https://api.appota.com/banner/get?access_token="
				+ access_token + "&lang=vi";
		String rs = post(url, list);

		return rs;
	}

	public static String getAccesToken() {
		String result = "";
		String url_request = API.ACCESS_TOKEN_URL;
		ArrayList<NameValuePair> nameValuePairs = addParameter();
		result = post(url_request, nameValuePairs);
		return result;

	}

	public static ArrayList<NameValuePair> addParameter() {
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		String requestToken = JsonUtil.getRequestToken(getRequestToken());
		Log.d(TAG, requestToken);
		list.add(new BasicNameValuePair(CommonStatic.REQUEST_TOKEN,
				requestToken));
		list.add(new BasicNameValuePair(CommonStatic.CLIENT_ID,
				CommonStatic.CLIENT_ID_VALUE));
		list.add(new BasicNameValuePair(CommonStatic.CLIENT_SECRET,
				CommonStatic.CLIENT_SECRET_VALUE));
		list.add(new BasicNameValuePair(CommonStatic.REDIRECT_URI,
				CommonStatic.REDIRECT_URI));
		list.add(new BasicNameValuePair(CommonStatic.GRANT_TYPE,
				CommonStatic.GRANT_TYPE_VALUE));
		list.add(new BasicNameValuePair(CommonStatic.LANG,
				CommonStatic.GRANT_TYPE_VALUE));
		return list;
	}

	public static String checkUpdate(Context context, String client) {
		String lang = Locale.getDefault().getLanguage();
		if (!lang.equalsIgnoreCase("vi")) {
			lang = "en";
		}
		String result = "";
		String url_request = API.PLAY_OTA_CHECKUPDATE + "?"
				+ CommonStatic.ACCESS_TOKEN + "="
				+ JsonUtil.getAccessToken(getAccesToken()) + "&lang=" + lang;
		Log.d(TAG, url_request);
		String version = CommonUtils.getClientVersion(context);
		int build = CommonUtils.getClientVersionCode(context);
		String device_id = CommonUtils.getDeviceId(context);
		int machine_status = 0;
		Log.e("version", version);
		Log.e("client", client);
		Log.e("build", String.valueOf(build));
		Log.e("device_id", device_id);
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("client", client));
		nameValuePairs.add(new BasicNameValuePair("version", version));
		nameValuePairs.add(new BasicNameValuePair("build", String
				.valueOf(build)));
		nameValuePairs.add(new BasicNameValuePair("device_id", device_id));
		result = post(url_request, nameValuePairs);
		Log.e(TAG, result);
		return result;
	}

}
